package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.common.enums.ordercenter.DictionaryTypeEnum;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BusinessUnitAbilityAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.DictionaryVerifyAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.BusinessUnitDTO;
import com.alibaba.cloud.retail.customer.center.ability.BusinessUnitAbilityService;
import com.alibaba.cloud.retail.customer.center.resp.BusinessUnitResp;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName BusinessUnitAbilityAdapterImpl
 * @description: 查询业务渠道信息
 * @date 2023/06/04 21:36
 * @version: 1.0
 */
@Service
public class BusinessUnitAbilityAdapterImpl implements BusinessUnitAbilityAdapter {
    private static final Log log = Log.getLogger(BusinessUnitAbilityAdapterImpl.class);

    @DubboReference
    private BusinessUnitAbilityService businessUnitAbilityService;
    @Resource
    private DictionaryVerifyAdapter dictionaryVerifyAdapter;

    @Override
    public List<BusinessUnitDTO> queryBusinessUnit(List<String> businessUnitCodeList) {
        if(CollectionUtils.isEmpty(businessUnitCodeList)) {
            return Lists.newArrayList();
        }
        List<BusinessUnitResp> businessUnitByCodeListForOrders =
                businessUnitAbilityService.findBusinessUnitByCodeListForOrder(businessUnitCodeList);

        log.info("业务单元查询 request={}, result={}", JSON.toJSONString(businessUnitCodeList),
                JSONArray.toJSONString(businessUnitByCodeListForOrders));

        if(Objects.isNull(businessUnitByCodeListForOrders) || CollectionUtils.isEmpty(businessUnitByCodeListForOrders)) {
            return Lists.newArrayList();
        }
        return businessUnitByCodeListForOrders.stream().map(p->{
            BusinessUnitDTO queryDTO = new BusinessUnitDTO();
            BeanUtils.copyProperties(p, queryDTO);
            return queryDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public Boolean checkBusinessUnitCode(String businessUnitCode) {
        try {
            dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode(), businessUnitCode, new FunctionException("OC-01-001-01-15-006", "业务单元编码"));
        } catch (FunctionException e) {
            log.error("业务单元字典校验异常", e);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
